package com.gitee.hljdrl.uikit.basic;

import android.view.View;

import com.gitee.hljdrl.uikit.UIKitOption;

/**
 * 实现 View.OnClickListener接口
 * 实现功能：
 * 1.实现防重点击。
 */
public class OnUIClickListener implements View.OnClickListener {


    private final long MIN_TIME = UIKitOption.getInstance().getOptionAsLong(UIKitOptionEvent.OPTION_DOUBLE_CLICK_TIME,1000);

    /********防止按钮连续点击********/
    private long mLastClickTime;

    private View.OnClickListener mOnClickListener;

    public OnUIClickListener() {
    }

    public OnUIClickListener(View.OnClickListener clickListener) {
        mOnClickListener = clickListener;
    }

    public void setOnClickListener(View.OnClickListener listener) {
        mOnClickListener = listener;
    }

    @Override
    public void onClick(View view) {
        if (mOnClickListener == null) {
            return;
        }
        if (!isFastClick()) {
            if (mOnClickListener != null) {
                mOnClickListener.onClick(view);
            }
        }
    }

    private synchronized boolean isFastClick() {
        long time = System.currentTimeMillis();
        if (time - mLastClickTime < MIN_TIME) {
            return true;
        }
        mLastClickTime = time;
        return false;
    }
}
